package com.meitianhui.convenitenbuy.module.home.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.home.contract.HomeScanningContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.MemberInfo;
import com.meitianhui.convenitenbuy.module.home.model.bean.OfficialStore;
import com.meitianhui.convenitenbuy.module.home.ui.activity.HomeScanCodeActivity;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.StoreImageBean;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.util.LogUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeScanningPresenter implements HomeScanningContract.Presenter {

    private HomeScanCodeActivity context;
    private final HomeScanningContract.View view;
    private final Gson gson;

    public HomeScanningPresenter(HomeScanCodeActivity view, HomeScanCodeActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getMemberInfo(String phone) {
        String userToken = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "member.memberInfoFindByMobile";
        Map<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("member_type_key", "consumer");
        String jsonParams = gson.toJson(map);

        HomeApiDataSource.getInstance()
                .getMemberInfo(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<MemberInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<MemberInfo> response) {
                        view.getMemberInfoBack(response.data, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getMemberInfoBack(null, msg);
                    }
                });
    }

    @Override
    public void getStoreInfo(String storeId) {
        String userToken = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "stores.stores.storesBaseInfoFind";
        Map<String, String> map = new HashMap<>();
        map.put("stores_id", storeId);
        String jsonParams = gson.toJson(map);

        HomeApiDataSource.getInstance()
                .getStoreInfo(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<OfficialStore>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<OfficialStore> response) {
                        view.getStoreInfoBack(response.data, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getStoreInfoBack(null, msg);
                    }
                });
    }

    @Override
    public void getMemberPortrait(String path) {
        String appToken = InfoProvider.getAppToken(App.getInstance());
        MineApiDataSource.getInstance()
                .getHeadImg(appToken, path)
                .subscribe(new ResponseObserver<List<StoreImageBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(List<StoreImageBean> storeImageBean) {
                        if (storeImageBean != null && !storeImageBean.isEmpty()) {
                            String doc_path = storeImageBean.get(0).getDoc_path();
                            LogUtils.e("==============url" + doc_path);
                            view.getPortraitBack(doc_path);
                        }
                    }
                });
    }

    @Override
    public void collectStore(OfficialStore bean) {
        String appToken = InfoProvider.getAppToken(App.getInstance());
        String service = "stores.consumer.favoriteStore";
        Map<String, String> hashmap = new HashMap<>();
        hashmap.put("stores_id", bean.getStores_id());
        hashmap.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        String jsonParams = gson.toJson(hashmap);

        HomeApiDataSource.getInstance()
                .collectStore(appToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.collectStoreBack(null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.collectStoreBack(msg);
                    }
                });
    }

}
